package com.itlong.cloud.operate.service.impl;


import com.itlong.cloud.POJO.PO.OperationSpecialLogPO;
import com.itlong.cloud.operate.dao.OperationSpecialLogDao;
import com.itlong.cloud.operate.service.OperationSpecialLogService;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
public class OperationSpecialLogServiceImpl implements OperationSpecialLogService {

    @Autowired
    private OperationSpecialLogDao operationSpecialLogDao;


    /**
     * <desc>
     * 分页获取操作日志
     * </desc>
     *
     * @Author chengshican
     * @Date 2020-11-10 18:33
     * @Param
     * @Return
     * @Exception
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperationSpecialLogPO> getOperationSpecialLog(String outerId, String pageSize, String currentPage,String orderColumn,String orderRule) throws Exception {

        //将DTO转换为Map params
        Map<String, Object> params = new HashMap<>();
        if(StringUtils.isBlank(orderColumn)){
            params.put("orderColumn","createTime");
            params.put("orderRule","DESC");
        }else if ("createDate".equals(orderColumn)){
            params.put("orderColumn","createTime");
            params.put("orderRule",orderRule);
        }

        params.put("outerId", outerId);
        //分页类默认pageSize为20
        Page<OperationSpecialLogPO> page = new Page<>();

        page.getPage().setPageSize(pageSize==null? "20":pageSize);
        page.getPage().setCurrentPage(currentPage==null? "1":currentPage);
        params.put("currentPage", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        //填充返回数据集
        page.setRows(operationSpecialLogDao.getOperationSpecialLog(params), operationSpecialLogDao.getOperationSpecialLogCount(params));
        return page;
    }

    @Override
    public OperationSpecialLogPO getOperationSpecialLogByLogId(String LogId) throws Exception {
        return operationSpecialLogDao.getOperationSpecialLogByLogId(LogId);
    }
}
